package com.ruoyi.project.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.base.domain.ComponentClassification;
import com.ruoyi.base.service.IComponentClassificationService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.contract.domain.ContractInfo;
import com.ruoyi.contract.service.IContractInfoService;
import com.ruoyi.project.domain.ProjectModelInfo;
import com.ruoyi.project.vo.FloorInfo;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.data.repository.query.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.domain.ProjectInfo;
import com.ruoyi.project.service.IProjectInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目信息Controller
 * 
 * @author ruoyi
 * @date 2022-08-26
 */
@RestController
@RequestMapping("/Project/ProjectInfo")
public class ProjectInfoController extends BaseController
{
    @Autowired
    private IProjectInfoService projectInfoService;

    @Autowired
    private IContractInfoService contractInfoService;

    @Autowired
    private IComponentClassificationService componentClassificationService;

    /**
     * 查询项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProjectInfo projectInfo)
    {
        startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectInfoList(projectInfo);
        return getDataTable(list);
    }

    /**
     * 导出项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:export')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectInfo projectInfo)
    {
        List<ProjectInfo> list = projectInfoService.selectProjectInfoList(projectInfo);
        ExcelUtil<ProjectInfo> util = new ExcelUtil<ProjectInfo>(ProjectInfo.class);
        util.exportExcel(response, list, "项目信息数据");
    }

    /**
     * 获取项目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(projectInfoService.selectProjectInfoById(id));
    }

    /**
     * 新增项目信息
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:add')")
    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectInfo projectInfo) {
        checkRequiredFields(projectInfo);
        return toAjax(projectInfoService.insertProjectInfo(projectInfo));
    }

    /**
     * 修改项目信息
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:edit')")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectInfo projectInfo) {
        checkRequiredFields(projectInfo);
        return toAjax(projectInfoService.updateProjectInfo(projectInfo));
    }
    //校验必填项
    private void checkRequiredFields(ProjectInfo projectInfo){
        if (StringUtils.isBlank(projectInfo.getOrderType())){
            throw new ServiceException("订单类型不能为空!");
        }
        if (StringUtils.isBlank(projectInfo.getContractNo())){
            throw new ServiceException("合同编号不能为空!");
        }
        if (StringUtils.isBlank(projectInfo.getProjectName())){
            throw new ServiceException("项目名称不能为空!");
        }
        if (projectInfo.getStructCount() == null){
            throw new ServiceException("结构量不能为空!");
        }
        if (StringUtils.isBlank(projectInfo.getOppositeCompany())){
            throw new ServiceException("对方单位不能为空!");
        }
        if (StringUtils.isBlank(projectInfo.getProjectAddr())){
            throw new ServiceException("项目地址不能为空!");
        }
    }

    /**
     * 删除项目信息
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:remove')")
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(projectInfoService.deleteProjectInfoByIds(ids));
    }

    /**
     * 获取项目下楼栋列表
     * @param projectNo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:FloorList')")
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
    @GetMapping(value = "/getFloorlist/{projectNo}")
    public AjaxResult getFloorList(@PathVariable("projectNo") String projectNo){
        return AjaxResult.success(projectInfoService.getFloorList(projectNo));
    }

    /**
     * 创建楼栋
     * @param floorInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:addBuild')")
    @PostMapping(value = "/createBuild")
    public AjaxResult createFloor(@RequestBody FloorInfo floorInfo){
        return toAjax(projectInfoService.createFloor(floorInfo));
    }

    /**
     * 删除楼栋
     * @param floorInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:dropBuild')")
    @PostMapping(value = "/delBuild")
    public AjaxResult dropFloor(@RequestBody FloorInfo floorInfo){
        return toAjax(projectInfoService.delFloor(floorInfo));
    }



    /**
     * 复制楼栋
     * @param floorInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:copyBuild')")
    @PostMapping(value = "/copyBuild")
    public AjaxResult copyFloorList(@RequestBody FloorInfo floorInfo){
        return toAjax(projectInfoService.copyFloor(floorInfo));
    }


    /**
     * 获取楼层构件——楼层表
     * @return
     */
    @PostMapping("/getFloorLayer")
    public AjaxResult getFloorLayer(@RequestBody FloorInfo floorInfo){
        AjaxResult floorLayer = projectInfoService.getFloorLayer(floorInfo);
        return AjaxResult.success(floorLayer);
    }

    @GetMapping("/getCompomentTypeList")
    public TableDataInfo list(ComponentClassification componentClassification) {
        List<ComponentClassification> list = componentClassificationService.selectComponentClassificationList(componentClassification);
        return getDataTable(list);
    }

    /**
     * 获取构件下拉框
     */
    @PostMapping("/getCompomentList")
    public AjaxResult getCompomentList(@RequestBody FloorInfo floorInfo){
        if (StringUtils.isEmpty(floorInfo.getProjectNo())) {
            return AjaxResult.error("项目编号不可为空！");
        }
        return AjaxResult.success(projectInfoService.getCompomentList(floorInfo));
    }

    /**
     * 添加构件
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:addWidget')")
    @PostMapping("/addWidget")
    public AjaxResult addWidget(@RequestBody FloorInfo floorInfo){
        return toAjax(projectInfoService.addWidget(floorInfo));
    }

    /**
     * 复制层
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:copyLayer')")
    @PostMapping("/copyLayer")
    public AjaxResult copyLayer(@RequestBody FloorInfo floorInfo){
        return toAjax(projectInfoService.copyLayer(floorInfo));
    }

    /**
     * 删除层
     * @param floorInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:delLayer')")
    @PostMapping("/delLayer")
    public AjaxResult delLayer(@RequestBody FloorInfo floorInfo){
        return toAjax(projectInfoService.delLayer(floorInfo));
    }
    /**
     * 模板下载
     * @return
     */
    @PostMapping("/importTemplate")
    public void templateDownload(HttpServletResponse response) throws IOException {
        projectInfoService.templateDownload(response);
    }

    /**
     * 模板导入
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:importData')")
    @PostMapping("/importData")
    public AjaxResult templateExport(MultipartFile file, String projectNo, String buildNo) throws IOException {
        if (StringUtils.isBlank(projectNo) || StringUtils.isBlank(buildNo)){
            return AjaxResult.error("空参传入！");
        }
        projectInfoService.templateExport(file,projectNo,buildNo);
        return AjaxResult.success();
    }

    /**
     * 导出项目构件信息列表
     */
    @PreAuthorize("@ss.hasPermi('ProjectInfo:ProjectInfo:export')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportMember")
    public void exportMember(HttpServletResponse response,ProjectInfo projectInfo) throws IOException {
        projectInfoService.exportMember(response,projectInfo);
    }

    /**
     * 获取项目列表
     */
    /**
     * 查询合同信息列表
     */
    @GetMapping("/getContractInfoList")
    public TableDataInfo getContractInfoList() {
        List<ContractInfo> list = projectInfoService.getContractInfoList();
        return getDataTable(list);
    }

    /**
     * 返回合同相关信息
     */
    @GetMapping(value = "/getContractInfo/{id}")
    public AjaxResult getContractInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(projectInfoService.selectContractInfoById(id));
    }
}
